package com.danielschon.triangularity.sprites;

import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import com.danielschon.triangularity.utility.Vec;

public class Bullet extends Sprite{

	public interface OnTimeoutListener{
		public void onTimeout(Bullet b);
	}
	
	
	public static final int LIFECYCLE = 60;
	
	public Vec location;
	public Vec movement;
	private int age = 0;
	@SuppressWarnings("unused")
	private Ship ship;
	private OnTimeoutListener listener;
	
	public Bullet(float pX, float pY, ITextureRegion pTextureRegion,
			VertexBufferObjectManager vertexBufferObjectManager, Vec m, Ship s) {
		super(pX, pY, pTextureRegion, vertexBufferObjectManager);
		location = new Vec(pX, pY);
		movement = m;
		ship = s;
	}
	
	@Override
	public void onManagedUpdate(float pSecondsElapsed){
		location.add(movement);
		this.setPosition(location.x, location.y);
		age++;
		if (age > LIFECYCLE){
			timeout();
		}
	}
	
	public void setOnTimeoutListener(OnTimeoutListener otl){
		listener = otl;

	}
	
	private void timeout(){
		if (listener != null){
			listener.onTimeout(this);
		}
	}
	
}
